package com.lzc.controller;

import com.lzc.pojo.vo.Result;
import com.lzc.service.FileUpLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping("/im/file")
public class FileUpLoadController {

    @Autowired
    private FileUpLoadService fileUpLoadService;

    /**
     * 浏览器上传头像到服务器
     * @param file
     * @return
     */
    @PostMapping("/upload/head/img")
    @ResponseBody
    public Result uploadHeadImg(@RequestPart("file") MultipartFile file){
        return fileUpLoadService.upLoadHeadImg(file);
    }

    /**
     * 浏览器获取头像
     */
    @GetMapping("/load/head/img/{imgName}")
    public void loadHeadImg(@PathVariable("imgName") String imgName, HttpServletResponse response){

        fileUpLoadService.loadHeadImg(imgName, response);

    }

    /**
     * 浏览器上传图片到素材库
     * @param files
     * @return
     */
    @PostMapping("/upload/img")
    @ResponseBody
    public Result uploadImg(@RequestPart("file") MultipartFile[] files){
        return fileUpLoadService.upLoadImg(files);
    }

    /**
     * 查看素材库的图片
     */
    @GetMapping("/load/img/{imgName}")
    public void loadImg(@PathVariable("imgName") String imgName, HttpServletResponse response){
        fileUpLoadService.loadImg(imgName,response);
    }

    /**
     * 查看文章
     */
    @GetMapping("/scan/article/**")
    public void loadArticle(HttpServletRequest request,HttpServletResponse response){
        fileUpLoadService.loadArticle(request,response);
    }

}
